package com.xinfu.newproject.ui.pay_cost;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.xinfu.newproject.R;
import com.xinfu.newproject.adapter.paycost_adapter.PropertyFeeAdapter;
import com.xinfu.newproject.base.BaseActivity;
import com.xinfu.newproject.bean.certifition.ResidentBean;
import com.xinfu.newproject.bean.certifition.ResidentCertificationBean;
import com.xinfu.newproject.bean.paycost.bean.GetBillByTypeBean;
import com.xinfu.newproject.bean.paycost.bean.QueryTypesBean;
import com.xinfu.newproject.ui.pay_detail.BillingDetailsFragment;
import com.xinfu.newproject.presenter.PayCostPresenter;
import com.xinfu.newproject.util.BToast;
import com.xinfu.newproject.view.IView;

import java.math.BigDecimal;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

public class PropertyFeeActivity extends BaseActivity implements IView.IPayCostView, CompoundButton.OnCheckedChangeListener, PropertyFeeAdapter.PayCostCheckLisener {

    @BindView(R.id.activity_property_fee_rv)
    RecyclerView mRecyclerView;

    @BindView(R.id.activity_property_fee_cb)
    CheckBox checkBox;

    @BindView(R.id.activity_property_fee_money_tv)
    TextView moneyTv;

    private PropertyFeeAdapter propertyFeeAdapter;

    private PayCostPresenter payCostPresenter = new PayCostPresenter(this);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_property_fee);
        ButterKnife.bind(this);
        registerBack();
        initViewBind();
        initLisener();
    }

    private void initViewBind() {
        setActivityTitle("缴费");
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        propertyFeeAdapter = new PropertyFeeAdapter(this);
        mRecyclerView.setAdapter(propertyFeeAdapter);
    }

    private void initLisener() {
        checkBox.setOnCheckedChangeListener(this);
    }

    int tenantId;
    String roomId, type, cusOrRent;

    @Override
    protected void onResume() {
        super.onResume();
        if (getIntent().getExtras() != null) {
            String response = getIntent().getExtras().getString("response");
            ResidentBean residentBeans = new Gson().fromJson(response, ResidentBean.class);
            ResidentCertificationBean.ResultBean.RoomCheckUserListBean video = (ResidentCertificationBean.ResultBean.RoomCheckUserListBean) residentBeans.t;
            tenantId = video.getTenantId();
            roomId = video.getRoomId();

            if (video.getIdType() == 2) {
                cusOrRent = "2";
            } else {
                cusOrRent = "1";
            }
            type = getIntent().getExtras().getString("type");
            payCostPresenter.getBillsByType();
        }
    }

    @OnClick({R.id.activity_property_fee_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.activity_property_fee_submit:
                if (totleMoney > 0) {
                    String billids = "";
                    if (getBillByTypeBean != null) {
                        for (int i = 0; i < getBillByTypeBean.getResult().size(); i++) {
                            if (getBillByTypeBean.getResult().get(i).isCheck()) {
                                billids = billids + "," + getBillByTypeBean.getResult().get(i).getId();
                            }
                        }
                    }
                    if (String.valueOf(billids.charAt(0)).equals(",")) {
                        billids = billids.substring(1, billids.length());
                    }
//                    if (String.valueOf(billids.charAt(billids.length()-1)).equals(",")){
//                        billids = billids.substring(0,billids.length()-1);
//                    }
                    Log.i("testString", billids);
                    Bundle bundle = new Bundle();
                    bundle.putString("tenantId", String.valueOf(tenantId));
                    bundle.putString("billType", type);
                    bundle.putString("billids", billids);
                    bundle.putDouble("payFee", totleMoney);
                    bundle.putString("roomId", roomId);

                    startIntentActivity(BillingDetailsFragment.class, bundle);
                } else {
                    BToast.showText(this, "请选择账单");
                }
                break;
        }
    }


    @Override
    public String header() {
        return String.valueOf(tenantId);
    }

    @Override
    public String type() {
        return type;
    }

    @Override
    public String roomId() {
        return roomId;
    }

    @Override
    public String cusOrRent() {
        return cusOrRent;
    }

    @Override
    public void showToast(String content) {
        BToast.showText(this, content);
    }

    @Override
    public void closeDispose(Disposable disposable) {

    }

    @Override
    public void showProgress() {
//        showLoding("");
    }

    @Override
    public void hideProgress() {
//        hideLoding();
    }

    @Override
    public void getData(QueryTypesBean queryTypesBean) {

    }

    GetBillByTypeBean getBillByTypeBean;

    @Override
    public void getData(GetBillByTypeBean getBillByTypeBean) {
        if (getBillByTypeBean.getResult() != null) {
            this.getBillByTypeBean = getBillByTypeBean;
            if (getBillByTypeBean.getResult().size() > 0) {
                checkBox.setEnabled(true);
            } else {
                checkBox.setEnabled(false);
            }
            propertyFeeAdapter.setNewData(getBillByTypeBean.getResult());
        } else {
            checkBox.setEnabled(false);
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        if (getBillByTypeBean != null) {
            if (b) {
                for (int i = 0; i < getBillByTypeBean.getResult().size(); i++) {
                    getBillByTypeBean.getResult().get(i).setCheck(true);
                }
                propertyFeeAdapter.setNewData(getBillByTypeBean.getResult());
            } else {
                for (int i = 0; i < getBillByTypeBean.getResult().size(); i++) {
                    getBillByTypeBean.getResult().get(i).setCheck(false);
                }
                propertyFeeAdapter.setNewData(getBillByTypeBean.getResult());
            }
        }
    }

    double totleMoney = 0.00;

    @Override
    public void checkMoneyLisener(int position, boolean isCheck, double money) {

        if (getBillByTypeBean != null) {
            getBillByTypeBean.getResult().get(position).setCheck(isCheck);
            totleMoney = 0.00;
            for (int i = 0; i < getBillByTypeBean.getResult().size(); i++) {
                if (isCheck) {
                    if (i <= position) {
                        getBillByTypeBean.getResult().get(i).setCheck(true);
                    }
                } else {
                    if (i >= position) {
                        getBillByTypeBean.getResult().get(i).setCheck(false);
                    }
                }
            }
            for (int i = 0; i < getBillByTypeBean.getResult().size(); i++) {
                if (getBillByTypeBean.getResult().get(i).isCheck()) {
                    totleMoney = totleMoney + getBillByTypeBean.getResult().get(i).getZjJe();
                }
            }
            double v = new BigDecimal(totleMoney).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            moneyTv.setText("需付款  ¥ " + v);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    propertyFeeAdapter.setNewData(getBillByTypeBean.getResult());
                }
            }, 500);

        }

    }
}
